package net.hancl.modules.sys.service.impl;

import net.hancl.common.utils.copyo.BeanCopierUtils;
import net.hancl.modules.sys.bean.UserBean;
import net.hancl.modules.sys.dao.UserMapper;
import net.hancl.modules.sys.entity.User;
import net.hancl.modules.sys.service.IUserLoginService;
import net.hancl.modules.sys.utils.PassUtil;
import net.hancl.modules.sys.vo.UserLoginVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLoginService implements IUserLoginService {
    protected Logger logger = LoggerFactory.getLogger(UserLoginService.class);

    @Autowired
    private UserMapper userMapper;


    public UserBean login(UserLoginVo userLoginVo) {
        if(userLoginVo == null || StringUtils.isBlank(userLoginVo.getUserName()) || StringUtils.isBlank(userLoginVo.getPassword())){
            logger.warn("用户名密码为null 登陆！return");
            return null;
        }
        String password = PassUtil.encryptPassword(userLoginVo.getPassword(), userLoginVo.getUserName());
        userLoginVo.setPassword(password);

        User user = userMapper.query(userLoginVo);

        if(user==null){
            return null;
        }

        UserBean userBean=new UserBean();
        BeanCopierUtils.copyProperties(user, userBean);
        return userBean;
    }

    public User getUser(String username) {
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUserName(username);
        User user = userMapper.query(userLoginVo);
        return user;
    }

    public int logout(UserLoginVo userLoginVo) {
        return 0;
    }
}
